﻿using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.UC.款式异常;
using DXApplicationTangche.Utils;
using mendian;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DXApplicationTangche.DTO
{
    public class DTOMeasure
    {
        public string ITEM_CD { get; set; }
        public string ITEM_VALUE { get; set; }
        public string ITEM_NAME_CN { get; set; }
        public bool isFIT_VALUE { get; set; } //人尺寸值
        public bool isGarmentSize { get; set; } //成衣尺寸
        public bool isHalf { get; set; } //是否有1/2
        public DTOMeasure()
        {
        }

        public DTOMeasure(string iTEM_CD, string iTEM_VALUE, string iTEM_NAME_CN, bool isFIT_VALUE, bool isGarmentSize, bool isHalf)
        {
            ITEM_CD = iTEM_CD;
            ITEM_VALUE = iTEM_VALUE;
            ITEM_NAME_CN = iTEM_NAME_CN;
            this.isFIT_VALUE = isFIT_VALUE;
            this.isGarmentSize = isGarmentSize;
            this.isHalf = isHalf;
        }
        
    }

    public class DTOMeasureItem
    {
        public String id { get; set; }
        public String parentId { get; set; }
        public int index { get; set; }
        public Boolean hasFitValue { get; set; }
        public Boolean hasGarmentSize { get; set; }
        public Boolean hasHalf { get; set; }
        public double customerFitValue { get; set; }
        public string fitValue { get; set; }
        public double garmentSize { get; set; }
        public double inValue { get; set; }
        public string itemCd { get; set; }
        public double itemFitValue { get; set; }
        public string itemNameCn { get; set; }
        public string itemValue { get;set;}
        public double leastReasonable { get;set;}
        public double maxReasonable { get;set;}
        public double outValue { get; set; }
        public string propertyValue { get; set; }
        public DTOMeasureItem()
        {

        }

        public DTOMeasureItem(尺寸呈现dto dto)
        {
            this.customerFitValue = dto.CUSTOMER_FIT_VALUE;
            this.fitValue = dto.FIT_VALUE;
            this.garmentSize = dto.garmentSize;
            this.inValue = dto.IN_VALUE;
            this.itemCd = dto.ITEM_CD;
            this.itemFitValue = dto.ITEM_FIT_VALUE;
            this.itemNameCn = dto.ITEM_NAME_CN;
            this.itemValue = dto.ITEM_VALUE;
            this.leastReasonable = dto.leastReasonable;
            this.maxReasonable = dto.maxReasonable;
            this.outValue = dto.OUT_VALUE;
            this.propertyValue = dto.PROPERTY_VALUE;
        }

        public DTOMeasureItem(string id, string parentId, int index, bool hasFitValue, bool hasGarmentSize, bool hasHalf, double cUSTOMER_FIT_VALUE, string fIT_VALUE, double garmentSize, double iN_VALUE, string iTEM_CD, double iTEM_FIT_VALUE, string iTEM_NAME_CN, string iTEM_VALUE, double leastReasonable, double maxReasonable, double oUT_VALUE, string pROPERTY_VALUE)
        {
            this.id = id;
            this.parentId = parentId;
            this.index = index;
            this.hasFitValue = hasFitValue;
            this.hasGarmentSize = hasGarmentSize;
            this.hasHalf = hasHalf;
            this.customerFitValue = cUSTOMER_FIT_VALUE;
            this.fitValue = fIT_VALUE;
            this.garmentSize = garmentSize;
            this.inValue = iN_VALUE;
            this.itemCd = iTEM_CD;
            this.itemFitValue = iTEM_FIT_VALUE;
            this.itemNameCn = iTEM_NAME_CN;
            this.itemValue = iTEM_VALUE;
            this.leastReasonable = leastReasonable;
            this.maxReasonable = maxReasonable;
            this.outValue = oUT_VALUE;
            this.propertyValue = pROPERTY_VALUE;
        }
        public DTOMeasureItem SizeConflict()
        {
            if (this.inValue != 0)
            {
                this.inValue = 0;
            }
            if (this.outValue != 0)
            {
                this.outValue = 0;
            }
            return this;
        }
        /// <summary>
        /// 尺寸计算
        /// </summary>
        /// <returns></returns>
        public DTOMeasureItem CountSize()
        {
            this.garmentSize = this.itemFitValue + this.inValue - this.outValue;
            return this;
        }
        public DTOMeasureItem build尺寸增减值(String ColumnFieldName, String Value)
        {
            if (ColumnFieldName == "ITEM_FIT_VALUE")
            {
                this.itemFitValue = Convert.ToDouble(Value);
            }
            else if (ColumnFieldName == "IN_VALUE")
            {
                this.inValue = Convert.ToDouble(Value);
            }
            else if (ColumnFieldName == "OUT_VALUE")
            {
                this.outValue = Convert.ToDouble(Value);
            }
            else if (ColumnFieldName == "garmentSize")
            {
                this.garmentSize = Convert.ToDouble(Value);
            }
            return this;
        }

        public DTOMeasureItem build加减(String INV,String OUTV)
        {
            this.inValue = Convert.ToDouble(INV);
            this.outValue = Convert.ToSingle(OUTV);
            return this;
        }

    }
    public class DTOStyleMeasureItems
    {
        public List<DTOMeasureItem> measureItemDTOS { get; set; } = new List<DTOMeasureItem>();
        public string sessionId { get; set; }
        public DTOStyleMeasureItems()
        {

        }
        /// <summary>
        /// 排序
        /// </summary>
        public void SortFunction()
        {
            var result = measureItemDTOS.OrderBy(x => x.index);  //按照baisort属性正du序排zhi序，dao
                                                     //list.OrderByDescending(x+>x.Name);  //按照Name倒序排列
        }
        public void build计算(String id, String FieldName, String value)
        {
            //this.measureItemDTOS[RowHandle]  //  DTO
            //    .SizeConflict() //  尺寸冲突
            //    .build尺寸增减值(FieldName, value);   //  根据修改列修改尺寸值
            foreach(DTOMeasureItem d in this.measureItemDTOS)
            {
                if (d.id.Equals(id))
                {
                    d.SizeConflict() //  尺寸冲突
                .build尺寸增减值(FieldName, value);   //  根据修改列修改尺寸值
                }
            }
            if (FieldName != "garmentSize")
            {
                //  尺寸计算
                foreach (DTOMeasureItem dto in this.measureItemDTOS)
                {
                    dto.CountSize();
                }
            }
        }

        public DTOStyleMeasureItems build预览(DataRow dr)
        {
            string inv = dr["IN_VALUE"].ToString();
            return this;
        }
        public DTOStyleMeasureItems build算(String id, String FieldName, String value)
        {
            for (int i = 0; i < this.measureItemDTOS.Count; i++)
            {
                if (this.measureItemDTOS[i].id.Equals(id))
                {
                    if (FieldName == "inValue")
                    {
                        this.measureItemDTOS[i].inValue = Convert.ToDouble(value);
                    }
                    else if (FieldName == "outValue")
                    {
                        this.measureItemDTOS[i].outValue = Convert.ToDouble(value);
                    }
                    String json = FunctionHelper.JsonSerialization(this.measureItemDTOS[i]);
                    this.measureItemDTOS[i] = FunctionHelper.JsonDeserialization<DTOMeasureItem>(RestCall.PostHttp(URL.接口url + "/style/saveMeasureItem?sessionId=" + this.sessionId, json));
                    break;
                }
            }
            return this;
        }

        public Boolean hasGarmentSize(String fitValue)
        {
            foreach(DTOMeasureItem dTO in this.measureItemDTOS)
            {
                if (dTO.fitValue.Equals(fitValue))
                {
                    return dTO.hasGarmentSize;
                }
            }
            return false;
        }
        public Boolean hasFitValue(String customerFitValue)
        {
            foreach (DTOMeasureItem dTO in this.measureItemDTOS)
            {
                if (dTO.customerFitValue.Equals(customerFitValue))
                {
                    return dTO.hasFitValue;
                }
            }
            return false;
        }
    }
}
